import java.util.PriorityQueue;

class PriorotyQueue {
	private PriorityNode front;
	private PriorityNode rear;

	public PriorotyQueue() {
	}

	public void clear() {
		front = null;
		rear = null;
	}

	public boolean isEmpty() {
		if (front == null) {
			return true;
		} else {
			return false;
		}
	}

	public int length() {
		PriorityNode p = front;
		int length = 0;
		while (p != null) {
			p = p.getNext();
			length++;
		}
		return length;
	}

	public void offer(Object data, int priority) {
		PriorityNode newNode = new PriorityNode(data, priority);
		if (front == null) {
			front = rear = newNode;
		} else {
			PriorityNode lastNode = front;
			if (lastNode.getPriority() > priority) {
				front = newNode;
				newNode.setNext(lastNode);
			} else {
				while (lastNode.hasNext() && priority <= lastNode.getPriority()) {
					lastNode = lastNode.getNext();
				}
				if (lastNode.hasNext()) {
					PriorityNode nextNode = lastNode.getNext();
					lastNode.setNext(newNode);
					newNode.setNext(nextNode);
				} else {
					lastNode.setNext(newNode);
					rear = newNode;
				}
			}
		}
	}

	public Object peek() {
		if (front == null) {
			return null;
		} else {
			return front.getData();
		}
	}

	public Object poll() {
		if (front == null) {
			return null;
		} else {
			PriorityNode p = front;
			front = p.getNext();
			return front.getData();
		}
	}

	public String getString() {
		String pString;
		if (isEmpty()) {
			return "This prioroty queue is empty.";
		} else {
			PriorityNode p = front;
			pString = p.getData().toString();
			while (p != null) {
				p = p.getNext();
				pString = pString + " ," + p.getData().toString();
			}
		}
		return pString;
	}

	public PriorityNode getFront() {
		return front;
	}
}
